package lifeOfAFish.scenegraph.test;


import lifeOfAFish.test.JoglTemplate;
import java.io.File;

import javax.media.opengl.*;
//import javax.media.opengl.GLAutoDrawable;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;
import lifeOfAFish.scenegraph.*;
import lifeOfAFish.scenegraph.geom.*;


public class test_lightsource extends JoglTemplate {
	
	// scenegraph nodes
	sgNode root = new sgNode();
	sgGeomCube cube = new sgGeomCube();
	sgGeomMesh mesh = new sgGeomMesh();
	sgLight light = null;
	
	sgTransform rot = new sgTransform();
	sgTransform trans = new sgTransform();
	
	
	float theta = 0.0f;
	float theta2 = 0.0f;
	float step = 1.0f;
	
	
	public static void main (String[] args){
		test_lightsource template = new test_lightsource();
		template.setVisible(true);
		
	}

	public void init(GLAutoDrawable drawable)
	{
		super.init(drawable);
		
		// get gl object
		GL gl = drawable.getGL();
		sgGlobals.gl = gl;
		
		// enable depth testing
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glDepthRange(0.0, 1000.0);
		
		// enable lighting
		gl.glEnable(GL.GL_LIGHTING);
		gl.glShadeModel(GL.GL_SMOOTH);

		// construct the scenegraph tree.
		light = sgLightFactory.newLight();
		root.addChild(cube);
		
		root.addChild(rot);
		rot.addChild(trans);
		trans.translate(2.0f, 1.0f, 0.0f);
		trans.addChild(light);
		
		
		sgMaterial mat1 = new sgMaterial();
		mat1.setAmbient(0.5f, 0.1f, 0.1f, 1.0f);
		mat1.setDiffuse(0.4f, 0.0f, 0.0f, 1.0f);
		mat1.setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
		cube.setMaterial(mat1);
		System.out.println("mat1 : " + mat1);
		
		sgGeomSphere sphere = new sgGeomSphere(0.25f);
		
		sgMaterial sphereMat = new sgMaterial();
		sphereMat.setAmbient(1.0f, 1.0f, 1.0f, 1.0f);
		sphereMat.setDiffuse(1.0f, 1.0f, 1.0f, 1.0f);
		sphereMat.setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
		sphereMat.setEmission(1.0f, 1.0f, 1.0f, 1.0f);
		light.getPosition()[3] = 0.0f;
		sphere.setMaterial(sphereMat);
		System.out.println("sphereMat : " + sphereMat);
		light.addChild(sphere);
		light.setSpotCutoff(20.0f);
		
	}
	
	
	/*
	 * overwritten version of display
	 */
	public void display(GLAutoDrawable drawable)
	{
		// get the gl object
		GL gl = drawable.getGL();
		// set the erasing color (black)
		gl.glClearColor(0f, 0f, 0f, 0f);
		// clear screen with the defined erasing color and depth buffer
		
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glPushMatrix();
		applyMouseTranslation(gl);
		applyMouseRotation(gl);
		
		theta += step;
		rot.rotate(theta, 0.0f, 1.0f, 0.0f);
		
		// fire the traversal
		root.traverse();
		
		// delay for a while
//		double time = System.currentTimeMillis();
//		while (System.currentTimeMillis() - time < 80);
		
		
		// load an object using the mesh loader
		gl.glPopMatrix();
	}
	
	
}
